package  
{
	import Box2DAS.Dynamics.ContactEvent;
	import com.citrusengine.core.CitrusEngine;
	import com.citrusengine.objects.platformer.Hero;
	import com.citrusengine.view.spriteview.SpriteArt;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.setTimeout;
	import flash.utils.Timer;
	import org.osflash.signals.Signal;
	import State.GameState;
	
	import Box2DAS.Common.V2;
	import Box2DAS.Dynamics.b2Fixture;
	import Box2DAS.Dynamics.ContactEvent;
	import Box2DAS.Dynamics.b2Body;
	import com.citrusengine.core.CitrusEngine;
	import com.citrusengine.physics.CollisionCategories;
	import flash.display.MovieClip;
	import State.GameState;
	
	import com.citrusengine.math.MathVector;
	import com.citrusengine.objects.PhysicsObject;
	
	import flash.media.Video;
	import flash.ui.Keyboard;
	import flash.utils.clearTimeout;
	import flash.utils.getDefinitionByName;
	import flash.utils.setTimeout;
	
	/**
	 * ...
	 * @author Haxx
	 */
	public class Bird extends Hero
	{
		private var _life:int;
		public function get life():int { return _life; }
		public function set life(i:int):void { _life = i; }
		
		private var invincibleTime:Timer;
		private var isInvincible:Boolean;
		
		public var toggle_invi:Signal;
		
		public function Bird(name:String, params:Object = null) 
		{
			super(name, params);
			
			PlayerRecords.timesReincarnated += 1;
			
			this._life = PlayerUpgrades.lvlMaxLife + GameBalance.INITIAL_LIFE;
				
			isInvincible = false;
			toggle_invi = new Signal();
      }
	  
      override public function hurt():void
      {
         super.hurt();
         if (_life > 0 )
            _life--;
         
      }
	  
	  override public function destroy():void
	  {
		  toggle_invi.removeAll();
		  super.destroy();
	  }
      
      override protected function updateAnimation():void
      {
         if (_life > 0)
            super.updateAnimation();
			
         else
         {
            if (_animation != "die")
            {   
               _animation = "die";
               onAnimationChange.dispatch();
               controlsEnabled = false;
            }
         }
      }
      
      override protected function endHurtState():void
      {
         if (_life > 0)
         {
            _hurt = false;
            controlsEnabled = true;
			
			startInvincibleTimer();
         }
		 
      }
	  
	  private function startInvincibleTimer():void
	  {
		  invincibleTime = new Timer(2000, 1);
		  invincibleTime.addEventListener(TimerEvent.TIMER, invincibleTimeFinish);
		  invincibleTime.start();
		  isInvincible = true;
		  toggleAlpha();
	  }
	  
	  private function toggleAlpha():void
	  {
		  //when player is hit, fire signal to GameState to change the character's alpha
		  toggle_invi.dispatch();
	  }
	  
	  private function invincibleTimeFinish(e:TimerEvent):void
	  {
		  invincibleTime.stop();
		  invincibleTime.removeEventListener(TimerEvent.TIMER, invincibleTimeFinish);
		  invincibleTime = null;
		  isInvincible = false;
		  toggleAlpha();
	  }
	  
	  protected override function handleBeginContact(e:ContactEvent):void
		{
			var colliderBody:b2Body = e.other.GetBody();
			
			if (_enemyClass && colliderBody.GetUserData() is _enemyClass)
			{
				if (_body.GetLinearVelocity().y < killVelocity && !_hurt && !isInvincible)
				{
					hurt();
					
					//fling the hero
					var hurtVelocity:V2 = _body.GetLinearVelocity();
					hurtVelocity.y = -hurtVelocityY;
					hurtVelocity.x = hurtVelocityX;
					if (colliderBody.GetPosition().x > _body.GetPosition().x)
						hurtVelocity.x = -hurtVelocityX;
					_body.SetLinearVelocity(hurtVelocity);
				}
				else
				{
					_springOffEnemy = colliderBody.GetPosition().y * _box2D.scale - height;
					onGiveDamage.dispatch();
					
				}
			}
			
			
			//Collision angle
			if (e.normal) //The normal property doesn't come through all the time. I think doesn't come through against sensors.
			{
				var collisionAngle:Number = new MathVector(e.normal.x, e.normal.y).angle * 180 / Math.PI;
				if (collisionAngle > 45 && collisionAngle < 135)
				{
					_groundContacts.push(e.other);
					_onGround = true;
				}
			}
		}
	  
		
	}

}